<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- layout布局 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加菜品分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表单 -->
      <el-table
        ref="multipleTable"
        :data="classifyList"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div class="dishesBox">
              <el-tag
                v-for="(item, i) in scope.row.childrens"
                :key="i"
                class="dishes"
                type="success"
                >{{ item.dishname }}</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="classifyId" label="分类id"> </el-table-column>
        <el-table-column prop="classifyname" label="分类名称">
        </el-table-column>
        <el-table-column prop="clevel" label="分类等级">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.clevel == 0">一级标签</el-tag>
          </template>
        </el-table-column>

        <!-- 这里指定了一个宽度，才能撑下三个按钮 -->
        <el-table-column
          prop="address"
          label="操作"
          show-overflow-tooltip
          width="180px"
        >
          <!-- 这里也是使用了 插槽 -->
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteById(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加信息的对话框 -->
    <el-dialog
      title="添加菜品分类"
      :visible.sync="addDialogVisible"
      @close="addDialogClosed"
    >
      <el-form :model="addForm" ref="addFormRef" label-width="100px">
        <el-form-item label="菜品分类名称" prop="classifyname">
          <el-input v-model="addForm.classifyname"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSuccess">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑信息的对话框 -->
    <el-dialog
      title="添加菜品分类"
      :visible.sync="editDialogVisible"
      @close="editDialogClosed"
    >
      <el-form :model="addForm" ref="editFormRef" label-width="100px">
        <el-form-item label="菜品分类id" prop="classifyId">
          <el-input v-model="editForm.classifyId" disabled></el-input>
        </el-form-item>
        <el-form-item label="菜品分类名称" prop="classifyname">
          <el-input v-model="editForm.classifyname"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSuccess">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getClassifyList();
  },
  methods: {
    async getClassifyList() {
      const { data: res } = await this.$http.get("/classify/getTreeData");
      console.log(res, "树形结构");
      if (res.flag == true) {
        this.classifyList = res.data;
      } else {
        return this.$message.error("获取数据失败");
      }
    },
    async showEditDialog(row) {
      this.editDialogVisible = true;
      const { data: res } = await this.$http.get(
        `/classify/getById/${row.classifyId}`
      );
      console.log(res, "编辑回显菜品分类数据");
      if (res.flag == true) {
        this.editForm = res.data;
      } else {
        return this.$message.error("数据回显失败");
      }
    },
    deleteById(row) {
      console.log(row, "我是列数据用来删除的");
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete("/classify/delete/" + row.classifyId);
          this.getClassifyList();
          console.log(res, "我是删除之后的信息");
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async addSuccess() {
      const { data: res } = await this.$http.post("/classify/add", {
        classifyname: this.addForm.classifyname,
      });
      if (res.flag == true) {
        this.$message.success(res.msg);
        this.addDialogVisible = false;
        this.getClassifyList();
      } else {
        return this.$message.error(res.msg);
      }
      console.log(res, "添加分类之后的返回");
    },
    async editSuccess() {
      const { data: res } = await this.$http.put("/classify/update", {
        classifyId: this.editForm.classifyId,
        classifyname: this.editForm.classifyname,
      });
      console.log(res, "修改菜品分类之后的反馈数据");
      if(res.flag==true){
        this.editDialogVisible=false
        this.getClassifyList()
      }else{
        return this.$message.error(res.msg)
      }
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
  },
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      classifyList: [],
      addForm: {
        classifyname: "",
      },
      editForm: {
        classifyId: "",
        classifyname: "",
      },
    };
  },
};
</script>

<style scoped>
.dishesBox {
  display: flex;
  padding: 5px 15px;
}
.dishes {
  margin: 0 15px;
}
.el-pagination {
  margin-top: 15px;
}
.el-card {
  margin: 15px 0;
}
.el-table {
  margin-top: 15px;
}
</style>